#include "dndictionarydlg.h"
//#include <QHBoxLayout>
//#include <QVBoxLayout>
//#include <QMessageBox>

#include "tdomain.h"

DnDictionaryDlg::DnDictionaryDlg(QWidget *parent)
    : QDialog(parent)
{
    m_tDomain = static_cast<TDomain *>(QWidget::parentWidget());
    QHBoxLayout *hlayout = new QHBoxLayout(this);

    m_customTreeCtrl = new CustomTreeControl(this);

    m_text = new QPlainTextEdit();

    m_customTreeCtrl->m_db = m_tDomain->m_dbMySql->m_mysqlConn;


    m_customTreeCtrl->setTreeTitle("域名字典(只支持2级)");
    m_customTreeCtrl->m_treeType = "DnDiction";
    m_customTreeCtrl->initModel();

    QVBoxLayout *vlayout = new QVBoxLayout();
    m_btnSava = new QPushButton();
    m_btnSava->setText("保存");
    m_btnClear = new QPushButton();
    m_btnClear->setText("清除");
    m_btnClose = new QPushButton();
    m_btnClose->setText("关闭");

    vlayout->addWidget(m_btnSava);
    vlayout->addWidget(m_btnClear);
    vlayout->addWidget(m_btnClose);

    hlayout->addWidget(m_customTreeCtrl);
    hlayout->addWidget(m_text);
    hlayout->addLayout(vlayout);


    connect(m_customTreeCtrl, &CustomTreeControl::treeSelectedChanged, this, [&](int idx) {slotShowDnDictVal(idx);});
    connect(m_customTreeCtrl, &CustomTreeControl::treeDeleteNode, this, [&](int idx) {slotDelete(idx);});
    connect(m_btnSava, &QPushButton::clicked, this, &DnDictionaryDlg::slotSave);
    connect(m_btnClear, &QPushButton::clicked, this, &DnDictionaryDlg::slotClear);
    connect(m_btnClose, &QPushButton::clicked, this, &DnDictionaryDlg::slotClose);
}
DnDictionaryDlg::~DnDictionaryDlg()
{


}

void DnDictionaryDlg::slotDelete(int idx)
{
    qDebug()<<idx;
    QSqlQuery query(m_customTreeCtrl->m_db);
    query.prepare("delete FROM domain_dictionary WHERE dicttypeid = ?");
    query.addBindValue(idx);
    m_idx = idx;
    if (query.exec() ) {
        m_text->clear();

    }
    else
    {
        qDebug() << "Failed to delete  dicttext for dicttype ID: " << idx;
    }
}

void DnDictionaryDlg::slotShowDnDictVal(int idx)
{
    //qDebug()<<idx;
    m_text->clear();
    QSqlQuery query(m_customTreeCtrl->m_db);
    query.prepare("SELECT dicttext FROM domain_dictionary WHERE dicttypeid = ?");
    query.addBindValue(idx);
    m_idx = idx;
    if (query.exec() && query.next()) {
        QString dicttext = query.value(0).toString();
        m_text->setPlainText(dicttext);

    } else
    {

        //qDebug() << "Failed to select  dicttext for dicttype ID: " << idx;
        query.prepare("insert into  domain_dictionary(dicttypeid,dicttext) values (?,?)");

        query.addBindValue(idx);
        query.addBindValue("-");
        if (query.exec()) {

            //QMessageBox::information(this, "提示", "更新成功！");
            m_text->setPlainText("-");
        } else {
            qDebug() << "Failed to insert domain_dictionary id: " << m_idx;
            return ;
        }
    }
}

void DnDictionaryDlg::slotSave()
{
    qDebug()<<m_idx;
    if(m_idx==-1)
    {
        QMessageBox::information(this, "提示", "请选择类别");
        return;
    }
    QSqlQuery query(m_customTreeCtrl->m_db);
    query.prepare("update  domain_dictionary set dicttext = ? WHERE dicttypeid = ?");
    query.addBindValue(m_text->toPlainText());
    query.addBindValue(m_idx);

    if (query.exec()) {

        QMessageBox::information(this, "提示", "更新成功！");
    } else {
        qDebug() << "Failed to retrieve path for parent ID: " << m_idx;
        return ;
    }
}

void DnDictionaryDlg::slotClear()
{
    this->m_text->clear();
}

void DnDictionaryDlg::slotClose()
{
    this->close();
}
